using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using Common.Utilities;
using Common.Utilities.Dispatcher;


namespace Workflows.Components.FileManipulation
{
    [Serializable]
    public class FileLoopOutput:ResultBase
    {
        private List<string> _FilePaths;

        public List<string> FilePaths
        {
            get { return _FilePaths; }
            set { _FilePaths = value; }
        }
	
        public FileLoopOutput():base(Guid.Empty,Guid.Empty)
        {
            this._FilePaths = null;
        }

        public FileLoopOutput(Guid componentID, Guid jobTicketID, List<string> outputFiles) : base(componentID, jobTicketID)
        {
            this._FilePaths = outputFiles;
        }

        public override XmlNode Serialize(ref XmlDocument xDoc, ref XmlNode parentNode)
        {
            XmlNode xNode = XmlDataUtil.AddElement(ref xDoc, parentNode, "Output");
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "Type", this.GetType().FullName);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "WorkerID", this.OwnerWorkerInstanceID.ToString());
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "FinishTime", this.FinishTime.ToString());

            XmlNode fileRootNode = XmlDataUtil.AddElement(ref xDoc, xNode, "ReturnedFiles");
            if(this.FilePaths !=null && this.FilePaths.Count>0)
            {
                foreach(string filePath in this.FilePaths)
                {
                    XmlNode fileNode = XmlDataUtil.AddElement(ref xDoc, fileRootNode, "File");
                    XmlDataUtil.UpdateAttribute(ref xDoc,fileNode,"FilePath",filePath);
                }
            }

            return xNode;
        }

        public override ResultBase Instantiate(XmlNode xNode)
        {
            if (xNode.Attributes.GetNamedItem("Type").Value == this.GetType().FullName)
            {
                FileLoopOutput output = new FileLoopOutput();
                output.OwnerWorkerInstanceID = new Guid(xNode.Attributes.GetNamedItem("WorkerID").Value);
                output.FinishTime = DateTime.Parse(xNode.Attributes.GetNamedItem("FinishTime").Value);

                output.FilePaths = null;
                XmlNodeList fileNodes = xNode.SelectNodes("ReturnedFiles/File");
                if(fileNodes !=null && fileNodes.Count>0)
                {
                    output.FilePaths=new List<string>();
                    foreach(XmlNode fileNode in fileNodes)
                    {
                        string filePath = fileNode.Attributes.GetNamedItem("FilePath").Value;
                        output.FilePaths.Add(filePath);
                    }
                }

                return output;
            }
            else
            {
                throw new Exception("Unable to instantiate output: invalid type specified in xml node");
            }
        }
    }
}
